#include "pluginloader.h"
#include <QMessageBox>

PluginLoader::PluginLoader()
{

}

void PluginLoader::loadAllPlugins()
{
    QDir path(QDir::currentPath() + "/debug/addons");

    qDebug() << "current path is: " << path.currentPath();

    foreach (QString filename, path.entryList(QDir::Files))
    {
        qDebug() << "Testing for Plugin: " << path.absolutePath() + "/" + filename;
        loadPlugin(path.absolutePath() + "/" + filename);
    }
}

void PluginLoader::loadPlugin(QString filename)
{
    qDebug() << "Loading: " << filename;
    QPluginLoader loader(filename);
    QObject *possiblePlugin = loader.instance();

    if (possiblePlugin)
    {
        PluginInterface *plugin = qobject_cast<PluginInterface*>(possiblePlugin);
        if(plugin)
        {
            qDebug() << "Loaded: " << plugin->getName();

            plugin->doSomething();

        }
    }
    else
    {

        qDebug() << "Error: Not loaded. " << filename << " = " << loader.errorString();
        QMessageBox::warning(NULL, "Pluginerror", "Es ist ein Fehler beim laden des Plugins: <br>" + filename+ "</br> aufgetretten!\n Error: " + loader.errorString());
    }

}
